package com.gitee.wsl.mathematics.geometry.shape

import com.gitee.wsl.mathematics.geometry.d2.base.Direction
import kotlin.math.PI
import kotlin.math.abs
import kotlin.math.sqrt
import kotlin.math.tan


class Triangle(var p1: Point, var p2: Point, var p3: Point){

    fun getSideLines():List<Line>{
        return listOf(Line(p1,p2), Line(p2,p3), Line(p3,p1))
    }

    //判定是否在三角形内
    fun isInTriangle(
        x: Float, y: Float
    ): Boolean {
        return abs(getArea(p1, p2, p3) -
                (getArea(Point(x, y), p2, p3) +
                        getArea(p1, Point(x, y), p3) +
                        getArea(p1, p2, Point(x, y)))
        ) < 0.00001
    }

    //获取面积
    fun getArea(p1: Point, p2: Point, p3: Point): Float {
        val side1 = p1.distance(p3)
        val side2 = p1.distance(p2)
        val side3 = p2.distance(p3)
        val s = (side1 + side2 + side3) / 2
        return sqrt(s * (s - side1) * (s - side2) * (s - side3))
    }

    companion object{
        fun create(baseLine: Float,
                   baseLnCentX: Float,
                   baseLnCentY: Float,
                   direction: Direction = Direction.RIGHT): Triangle {
            // 计算偏移量
            val offset = (baseLine / 2 * tan(60 * PI / 180)).toFloat()
            return when (direction) {
                Direction.UP -> {
                    val p1= Point(baseLnCentX - baseLine / 2, baseLnCentY)
                    val p2= Point(baseLnCentX + baseLine / 2, baseLnCentY)
                    val p3= Point(baseLnCentX, baseLnCentY - offset)
                    Triangle(p1, p2, p3)
                }
                Direction.DOWN -> {
                    val p1= Point(baseLnCentX - baseLine / 2, baseLnCentY)
                    val p2= Point(baseLnCentX + baseLine / 2, baseLnCentY)
                    val p3= Point(baseLnCentX, baseLnCentY + offset)
                    Triangle(p1, p2, p3)
                }
                Direction.LEFT -> {
                    val p1= Point(baseLnCentX, baseLnCentY - baseLine / 2)
                    val p2= Point(baseLnCentX, baseLnCentY + baseLine / 2)
                    val p3= Point(baseLnCentX - offset, baseLnCentY)
                    Triangle(p1, p2, p3)
                }
                Direction.RIGHT -> {
                    val p1= Point(baseLnCentX, baseLnCentY - baseLine / 2)
                    val p2= Point(baseLnCentX, baseLnCentY + baseLine / 2)
                    val p3= Point(baseLnCentX + offset, baseLnCentY)
                    Triangle(p1, p2, p3)
                }
            }
        }

        fun create(width: Float, height: Float,
                   percentLeft: Float = 0f,
                   percentRight: Float = 0f,
                   percentBottom: Float = 0.5f): Triangle {
            val p1= Point(0f, percentLeft * height)
            val p2= Point(percentBottom * width, height)
            val p3= Point(width, percentRight * height)
            return Triangle(p1, p2, p3)
        }
    }
}